package com.examples.maps;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MySupportMapFragment extends SupportMapFragment{
	private View mOriginalContentView;
	private TouchableWrapper mTouchView;
	private MainActivity mActivity;
	private GoogleMap mMap;
	private MapCallback callback;
	
	public void setMapCallback(MapCallback callback){
		this.callback = callback;
	}
	
	 public static interface MapCallback
	  {
	     public void onMapReady(GoogleMap map);
	  }

	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
		mOriginalContentView = super.onCreateView(inflater, parent, savedInstanceState);
		mTouchView = new TouchableWrapper();
		mTouchView.addView(mOriginalContentView);
		return mTouchView;
	}
	
	@Override 
	public View getView(){
		return mOriginalContentView;
	}
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if(callback!= null) callback.onMapReady(getMap());      
	}
	
	class TouchableWrapper extends FrameLayout {
		public TouchableWrapper(){
			super(mActivity);
		}
		
		@Override
		public boolean dispatchTouchEvent(MotionEvent event){
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				 break;
			case MotionEvent.ACTION_UP: {
				int x = (int) event.getX();
				int y = (int) event.getY();
				mActivity.tapEvent(x,y);
				break;
				
			}
			}
			return super.dispatchTouchEvent(event);
		}
	}
}
